package com.xwh.shop.orders.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xwh.shop.orders.model.dto.CartDTO;
import com.xwh.shop.orders.model.entity.Cart;
import com.xwh.shop.orders.service.handlers.BaseContextHandler;
import com.xwh.shop.orders.service.mapper.CartMapper;
import com.xwh.shop.orders.service.service.CartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xwh
 * @since 2025-07-17
 */
@Slf4j
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {


    // 用于出异常后 将数据回滚到之前的状态
    @Transactional(rollbackFor = Exception.class)

    @Override
    public void addCart(CartDTO cartDTO) {
        Cart cart = new Cart();

        // 新的
        BeanUtils.copyProperties(cartDTO, cart);
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getGoodsId,cart.getGoodsId());
        wrapper.eq(Cart::getMemberId, cart.getMemberId());

        //原本的
        Cart item = this.getOne(wrapper);
        if(item != null){
            item.setQuantity(cart.getQuantity() + item.getQuantity());
            log.info("CartServiceImpl addCart:{}",item);
            this.updateById(item);
            return;
        }

        this.save(cart);
    }

    // todo 7.17 实现购物车条目的减少功能
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean reduceItemCart(CartDTO cartDTO) {
        log.info("CartServiceImpl reduceItemCart cartDTO:{}",cartDTO);
        if (cartDTO.getQuantity() <= 0) {
            log.info("CartServiceImpl reduceItemCart quantity <= 0");
            return false;
        }

        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getGoodsId,cartDTO.getGoodsId());
        wrapper.eq(Cart::getMemberId, cartDTO.getMemberId());

        Cart item = this.getOne(wrapper);
        if(item != null){
            Integer reduced  = item.getQuantity() - cartDTO.getQuantity();
            log.info("reduceItemCart reduced:{}",reduced);
            if(reduced <= 0){
                // 只删除这个商品的购物车
                return this.remove(wrapper);
            }

            item.setQuantity(reduced);
            log.info("reduceItemCart item:{}",item);
            return this.updateById(item);
        }
        log.info("CartServiceImpl reduceItemCart item is null");
        return false;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Cart updateCart(CartDTO cartDTO) {
        Cart cart = new Cart();
        BeanUtils.copyProperties(cartDTO, cart);

        this.updateById(cart);
        return cart;
    }

    @Override
    public List<Cart> getCart() {
        Long memberId = BaseContextHandler.get();
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getMemberId, memberId);
        List<Cart> cartList = this.list(wrapper);
        log.info("cartList:{}",cartList);
        return cartList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteCart(Long memberId) {

        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        log.info("CartServiceImpl deleteCart: memberId:{}",memberId);
        wrapper.eq(memberId != null,Cart::getMemberId, memberId);
        return this.remove(wrapper);
    }


}
